﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using Lab1;

namespace Lab1Tester
{
	class Program
	{
		private Backend endis;

		static void Main(string[] args)
		{
			new Program();
		}


		public Program()
		{
			string myConString = "SERVER=localhost;" +
				"DATABASE=ninjaloot2;" +
				"UID=root;" +
				"PASSWORD=jamessuger;";
			endis = new Backend(myConString);
			//TestCreateTable();
			TestInsertData();
			TestInsertBlob();
			//TestFetchData();

			Console.ReadLine();
		}

		private void TestFetchData()
		{
			var query = "select * from katter where id=0";
			DataTable dt = endis.GetData(query);

			FileStream fs = new FileStream("nybild.jpg",FileMode.OpenOrCreate,FileAccess.Write);
			BinaryWriter bw = new BinaryWriter(fs);

			bw.Write((byte[])dt.Rows[0]["bild"]);

		}

		private void TestInsertBlob()
		{
			FileStream fs = new FileStream("funnycat.jpg",FileMode.Open,FileAccess.Read);
			BinaryReader br = new BinaryReader(fs);
			byte[] minbild = br.ReadBytes((int)fs.Length);

			endis.StoreBlob("userfile", "file","1", minbild);
			Console.WriteLine("blob finns nu i databasen!");
		}

		public void TestCreateTable()
		{
			IDictionary<string,string> values = new Dictionary<string, string>();

			values.Add("id","INT");
			values.Add("färg","INT");
			values.Add("ålder", "INT");
			values.Add("namn","varchar(30)");
			values.Add("bild","BLOB");

			endis.CreateTable("katter",values);
			Console.WriteLine("Tabellen katter är nu skapad! .. tror jag.. mjauuu");
		}

		public void TestInsertData()
		{
			IDictionary<string, string> values = new Dictionary<string, string>();
			
			values.Add("id","1");
			values.Add("uid", "0");
			values.Add("name","'funnycat.jpg'");
			values.Add("size", "4321");

			endis.InsertData("userfile", values);
		}
	}
}
